package com.example.ccj.b.c.custom.selectuser;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.example.ccj.R;
import com.example.ccj.b.c.dialog.DialogBase;
import com.example.ccj.b.c.recycler.BaseRecycler;
import com.example.ccj.u.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 选择人员dialog
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/11/18 15:53
 */
public class OnlySelectUserDialog extends DialogBase {

    List<UserMode> userModeList;
    Map<Long,UserMode> selectedUserMap;
    List<UserMode> selectedUserList;
    BaseRecycler<UserMode> allUserRec;

    ISelectUserListener iSelectUserListener;
    public OnlySelectUserDialog(@NonNull Context context, List<UserMode> userModeList, List<UserMode> selectedUserList,ISelectUserListener iSelectUserListener) {
        super(context);
        this.userModeList = userModeList;
        this.selectedUserList = selectedUserList;
        this.iSelectUserListener = iSelectUserListener;
    }

    @Override
    public void onInit() {
        isCanClose(true);
        setLayout(R.layout.dialog_only_select_user);
        setAlpha(1f);//透明度
        isCanClose(true);//点击允许关闭
        isShowBackground(false);//不显示背景
        setShowHeightRatio(true);//按比例显示
        setHeightRatio(0.7);//高度显示比例
        showInBottom();//底部弹出
        setFull(true);
        goneTitleLeftImage();

        setTitleCenterText("选择人员");
        selectedUserMap = new HashMap<>();

        for (UserMode userMode : selectedUserList) {
            selectedUserMap.put(userMode.id,userMode);
        }

        allUserRec = findViewById(R.id.dialog_select_user_all);


        ddd(userModeList);

        //文本修改搜索
        EditText editText = findViewById(R.id.dialog_select_user_find);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                find(s.toString());
            }
        });

        //搜索
        ImageView findImg = findViewById(R.id.dialog_select_user_icon);
        findImg.setOnClickListener(v -> find(editText.getText().toString()));
    }

    private void find(String s){
        if (StringUtils.isEmpty(s)) {
            ddd(userModeList);
            return;
        }
        List<UserMode> userModes = new ArrayList<>();
        for (UserMode userMode : userModeList) {
            if (userMode.name.contains(s.toString())) {
                userModes.add(userMode);
            }
        }
        ddd(userModes);
    }


    private void ddd(List<UserMode> uuu){
        allUserRec.setAdapter(uuu,new AllSelectUserAdapter(uuu,selectedUserList, selectedUserMap, new ISelectUserListener() {
            @Override
            public void add(long userId, UserMode userMode) {
                selectedUserMap.put(userId,userMode);
                selectedUserList.add(userMode);
                iSelectUserListener.add(userId,userMode);
            }

            @Override
            public void delete(long userId, UserMode userMode) {
                UserMode remove = selectedUserMap.remove(userId);
                selectedUserList.remove(remove);
                iSelectUserListener.add(userId,remove);
            }
        }));
    }


}
